﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>8.如何使用BLL与DAL层</title>
    <script type="text/javascript" src="../Content/highlighter/scripts/shCore.js"></script>
    <script type="text/javascript" src="../Content/highlighter/scripts/shBrushCSharp.js"></script>
    <script type="text/javascript" src="../Content/highlighter/scripts/shBrushXml.js"></script>
    <link type="text/css" rel="stylesheet" href="../Content/highlighter/styles/shCoreDefault.css"/>
    <script type="text/javascript">SyntaxHighlighter.all();</script>
</head>
<body>
    <h3>如何使用BLL与DAL层</h3>
	<p>
        BLL逻辑操作层
    </p>
    <pre class="brush: csharp;">
        //声明一个类，并包含以下函数
        //其参数为con,即var con=Modles.Common.OpenConnection();中的con
        public List&lt;dynamic> GetAll(System.Data.IDbConnection con)
        {
            var con1 = Models.Common.GetConnection(con);
            var users = con1.QueryAll("select * from users");
            //当返回的是多行时其返回值类型为List&lt;dynamic>
            return users;
        }
        public dynamic GetRow(System.Data.IDbConnection con)
        {
            var con1 = Models.Common.GetConnection(con);
            var user = con1.QueryRow("select * from users where id=1");
            //当返回的是单行时其返回值类型为dynamic
            return user;
        }
        //其它情况下返回值是什么类型就写什么类型
        public List&lt;Models.Entity.users> GetAllUsers(System.Data.IDbConnection con)
        {
            var con1 = Models.Common.GetConnection(con);
            var users = con1.QueryAll&lt;Models.Entity.users>("select * from users");
            return users;
        }
        public Models.Entity.users GetRowUser(System.Data.IDbConnection con)
        {
            var con1 = Models.Common.GetConnection(con);
            var user = con1.QueryRow&lt;Models.Common.users>("select * from users where id=1");
            return user;
        }
    </pre>
    <p>
        DAL层会生成如下代码
    </p>
    <pre class="brush: csharp;">
        //此处仅列一项以做说明
        public NFinal.List&lt;__GetAll_users__> GetAll(System.Data.IDbConnection con)
        {
            var con1=(System.Data.SQLite.SQLiteConnection)con;
            #region	var users;选取所有记录
			var users = new NFinal.List&lt;__GetAll_users__>();
			var __GetAll_users_command__ = new System.Data.SQLite.SQLiteCommand("select * from users", con1);
			var __GetAll_users_reader__= __GetAll_users_command__.ExecuteReader();
			if (__GetAll_users_reader__.HasRows)
			{
				while (__GetAll_users_reader__.Read())
				{
					var __GetAll_users_temp__ = new __GetAll_users__();
					__GetAll_users_temp__.id = __GetAll_users_reader__.GetInt64(0);
					__GetAll_users_temp__.name =__GetAll_users_reader__.IsDBNull(1)?null: __GetAll_users_reader__.GetString(1);
					__GetAll_users_temp__.pwd =__GetAll_users_reader__.IsDBNull(2)?null: __GetAll_users_reader__.GetString(2);
					users.Add(__GetAll_users_temp__);
				}
			}
			__GetAll_users_reader__.Dispose();
			__GetAll_users_command__.Dispose();
			#endregion
            return users;
        }
    </pre>
    <p>
        Controller层的调用
    </p>
    <pre class="brush: csharp;">
        var con = Models.Common.OpenConnection();
        //DAL中的类声明
        Models.DAL.Sqlite sqlite = new Models.DAL.Sqlite();
        //使用由BLL生成的DAL类中的GetAll函数
        //数据库操作时，尽量减少数据库打开关闭的次数，
        //因为在.net中打开一次数据库的时间远远大于一般的查询操作。
        var users= sqlite.GetAll(con);
        con.Close();
    </pre>
</body>
</html>